package top.deepdesigner.controller.dashboard;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.deepdesigner.service.DtsGoodsProductService;
import top.deepdesigner.service.DtsGoodsService;
import top.deepdesigner.service.DtsOrderService;
import top.deepdesigner.service.DtsUserService;
import top.deepdesigner.util.ResponseUtil;
import top.deepdesigner.vo.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/20 星期二 17:41:18 <br/>
 */
@RestController
@RequestMapping("/admin/dashboard")
@CrossOrigin("*")
public class DashboardController {
  private static final int STATIS_DAYS_RANG = 30;// 统计的天数范围，一个月数据

  @Autowired
  private DtsUserService userService;
  @Autowired
  private DtsGoodsService goodsService;
  @Autowired
  private DtsGoodsProductService goodsProductService;
  @Autowired
  private DtsOrderService orderService;

  /**
   * 首页统计信息
   *
   * @return {@link Object}
   */
  @GetMapping("/")
  public Object info() {
    HashMap<String, Object> map = new HashMap<>();
    Long userCount = userService.getUserCount();
    Long goodsCount = goodsService.getGoodsCount();
    Long goodsProductCount = goodsProductService.getGoodsProductCount();
    Long orderCount = orderService.getOrderCount();
    map.put("userTotal", userCount);
    map.put("goodsTotal", goodsCount);
    map.put("productTotal", goodsProductCount);
    map.put("orderTotal", orderCount);
    return ResponseUtil.ok(map);
  }

  @GetMapping("/chart")
  public Object chart() {
    HashMap<String, Object> map = new HashMap<>();
    //1、销售统计情况
    CategorySellVo categorySellVo = new CategorySellVo();

    //销售统计数据
    List<CategorySellAmts> categorySellAmts = orderService.getCategorySell();
    //每天的订单总额和订单笔数
    List<DayStatis> orderTotalAndCountByDate = orderService.getOrderTotalAndCountByDate(STATIS_DAYS_RANG);
    //每天的用户增量
    List<DayStatis> userTotalAndCountByDate = userService.getUserTotalAndCountByDate(STATIS_DAYS_RANG);

    //1.1、创建分类名
    String[] categoryNames = new String[categorySellAmts.size()];
    for (int i = 0; i < categorySellAmts.size(); i++) {
      categoryNames[i] = categorySellAmts.get(i).getName();
    }
    //1.2、放入categorySellVo对象
    categorySellVo.setCategorySellData(categorySellAmts);
    categorySellVo.setCategoryNames(categoryNames);

    //2、日期对应的订单总额和订单笔数
    OrderAmtsVo orderAmtsVo = new OrderAmtsVo();

    //排一下顺序,这里通过sql语句查询
    //orderTotalAndCountByDate.sort(Comparator.comparing(DayStatis::getDayStr));
    String[] dayData = new String[orderTotalAndCountByDate.size()];
    BigDecimal[] orderAmtData = new BigDecimal[orderTotalAndCountByDate.size()];
    int[] orderCntData = new int[orderTotalAndCountByDate.size()];

    for (int i = 0; i < orderTotalAndCountByDate.size(); i++) {
      dayData[i] = orderTotalAndCountByDate.get(i).getDayStr();
      orderAmtData[i] = orderTotalAndCountByDate.get(i).getAmts();
      orderCntData[i] = orderTotalAndCountByDate.get(i).getCnts();
    }
    orderAmtsVo.setDayData(dayData);
    orderAmtsVo.setOrderAmtData(orderAmtData);
    orderAmtsVo.setOrderCntData(orderCntData);

    //3、日期对应的用户数和订单数
    UserOrderCntVo userOrderCntVo = new UserOrderCntVo();

    //3.1 日期数据
    String[] dayStringArray = insert2Array(userTotalAndCountByDate, orderTotalAndCountByDate);
    //日期数据、转换成String数组
    userOrderCntVo.setDayData(dayStringArray);
    //每日用户新增量
    userOrderCntVo.setUserCnt(insert2Array(dayStringArray, userTotalAndCountByDate));
    //每日订单量
    userOrderCntVo.setOrderCnt(insert2Array(dayStringArray, orderTotalAndCountByDate));

    map.put("categorySell", categorySellVo);
    map.put("orderAmts", orderAmtsVo);
    map.put("userOrderCnt", userOrderCntVo);
    return ResponseUtil.ok(map);
  }

  private int[] insert2Array(String[] dayList, List<DayStatis> targetList) {
    int[] count = new int[dayList.length];
    for (int i = 0; i < dayList.length - 1; i++) {
      for (int j = 0; j < targetList.size() - 1; j++) {
        if (dayList[i].equals(targetList.get(j).getDayStr())) {
          count[i] = targetList.get(j).getCnts();
        }
      }
    }
    return count;
  }

  private String[] insert2Array(List<DayStatis> orderList, List<DayStatis> userList) {
    HashSet<String> cacheDataData = new HashSet<>();
    //订单日期添加到HashSet中
    orderList.stream().map(item -> cacheDataData.add(item.getDayStr())).collect(Collectors.toSet());
    //用户日期添加到HashSet中
    userList.stream().map(item -> cacheDataData.add(item.getDayStr())).collect(Collectors.toSet());
    //排序并返回List集合
    List<String> dayDataList = cacheDataData.stream().sorted().collect(Collectors.toList());
    //日期数据、转换成String数组
    return dayDataList.toArray(new String[0]);
  }
}
